package com.example.service.impl;


import com.example.common.result.Result;
import com.example.entity.activiti.ApprovalVo;
import com.example.entity.activiti.FlowInfo;
import com.example.mapper.FlowMapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FlowService {
    @Autowired
    private ActFlowCommService actFlowCommService;
    @Autowired
    private FlowMapper flowMapper;

    public List<Task> findUserTask(Long userId) {
        List<Task> list = actFlowCommService.myTaskList(userId.toString());
        return list;
    }
    /**
     * 查询用户任务分页
     *
     */
    public Result<Map<String, ApprovalVo>> findUserTask(int current, int size, String userName, String formKey, Date dateAfter, Date dateBefore, String businessKey, String applicant) {
        return actFlowCommService.myTaskPage(current, size, userName, formKey, dateAfter, dateBefore, businessKey, applicant);
    }


    /**
     * 完成任务
     * @param
     */
    public void completeTask(String remark,String userName,String taskId,String advice) {
        actFlowCommService.completeProcess(remark,userName,taskId,advice);
    }

    /**
     * 查询所有流程
     * @return
     */
    public List<FlowInfo> findAllFlow() {
        return flowMapper.selectFlowList();
    }

    /**
     * 查询单个流程
     * @param id
     * @return
     */
    public FlowInfo findOneFlow(Long id) {
        return flowMapper.selectOneFlow(id);
    }

    /**
     * 更新部署状态
     * @param id
     * @return
     */
    public int updateDeployState(Long id){
        return flowMapper.updateFlowDeployState(id);
    }

  /*  @Autowired
    private UserService userService;
    @Autowired
    private SiteMessageService siteMessageService;
    *//**
     * 任务创建事件
     * @param delegateTask
     *//*
    public void createTaskEvent(DelegateTask delegateTask) {
        log.info("delegateTask=={}",delegateTask);
//        负责人
        String assignee = delegateTask.getAssignee();
//        获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.findOneUserByName(username);
        String userId = user.getId().toString();
//        任务id
        String taskId = delegateTask.getId();
        if(!assignee.equals(userId)){
            int type =1;
            siteMessageService.sendMsg(Long.valueOf(assignee),taskId,type,1);
        }

    }*/

    /**
     * 查询任务详细信息
     * @param taskId 任务id
     * @return Map 任务详情map
     */
 /*   public Object findTaskInfo(String taskId) {
        Object taskInfo = actFlowCommService.myTaskInfoList(taskId);
        return taskInfo;
    }
*/
    /*
    用户历史任务
     */
    public PageInfo findUserHisTask(int current, int size, String userName, String formKey, Date dateAfter, Date dateBefore, String businessKey, String applicant) {
        PageInfo pageInfo=actFlowCommService.searchUserHisTask(current,size,userName,formKey,dateAfter,dateBefore,businessKey,applicant);
        return pageInfo;
    }

    /**
     * 拾取任务
     *
     * @param taskId
     * @param userName
     * @return
     */
    public boolean claimTask(String taskId, String userName) {
        boolean b = actFlowCommService.claimTask(taskId, userName);
        return b;
    }

    /**
     * 组任务分页
     *
     * @param current
     * @param size
     * @param userName
     * @param formKey
     * @return
     */
    public PageInfo findGroupTask(int current, int size, String userName, String formKey) {
        //TODO 根据登入用户获取用户组或部门
        String groupName = "admin";
        if (userName.equals("test1")) {
            groupName = "group1";
        }
        if (userName.equals("test2")) {
            groupName = "group2";
        }
        PageInfo pageInfo = actFlowCommService.groupTaskPage(current, size, groupName, formKey);
        return pageInfo;
    }

    /**
     * 归还组任务
     *
     * @param taskId
     * @param userName
     * @return
     */
    public boolean returnTask(String taskId, String userName) {
        return actFlowCommService.returnTask(taskId, userName);
    }
}
